﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Windows7SlideshowWallpaperUtil
{
    public partial class WallpaperDisplay : UserControl
    {

        string currentWall = null;
        public string Wallpaper
        {
            get { return currentWall; }
            set
            {
                if (currentWall != value)
                {
                    Stream stream = null;
                    currentWall = value;
                    namelabel.Text = value;
                    try
                    {
                        stream = File.Open(currentWall, FileMode.Open, FileAccess.Read);
                        FileInfo file = new FileInfo(currentWall);
                        pictureBox1.Image = Image.FromStream(stream);
                        wlabel.Text = pictureBox1.Image.Width + "x"+pictureBox1.Image.Height;
                        slabel.Text = FileSizeFormat(file.Length);
                        button1.Enabled = true;
                        button2.Enabled = true;
                        button3.Enabled = true;
                        button4.Enabled = true;
                        button5.Enabled = true;
                    }
                    catch (Exception)
                    {
                        if (value == null || value == "")
                        {
                            namelabel.Text = "No wallpaper... yet..";
                            pictureBox1.Image = null;
                        }
                        else
                        {
                            pictureBox1.Image = Windows7SlideshowWallpaperUtil.Properties.Resources.NoImage;
                        }
                        button1.Enabled = false;
                        button2.Enabled = false;
                        button3.Enabled = false;
                        button4.Enabled = false;
                        button5.Enabled = false;
                        wlabel.Text = "";
                        slabel.Text = "";
                    }
                    finally
                    {
                        try
                        {
                            stream.Close();
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            }
        }

        public WallpaperDisplay()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Clipboard.SetData(DataFormats.Text, Wallpaper);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ShowSelectedInExplorer.FileOrFolder(Wallpaper);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(Wallpaper);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string wall = Wallpaper;
            if (DialogResult.Yes == MessageBox.Show("Are you sure you want to delete:\n" + wall, "Confirmation", MessageBoxButtons.YesNo))
            {
                Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(wall, Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs, Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin);
                button1.Enabled = false;
                button2.Enabled = false;
                button3.Enabled = false;
                button4.Enabled = false;
                button5.Enabled = false;
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                Windows7SlideshowWallpaperUtil.Properties.Settings settings = Windows7SlideshowWallpaperUtil.Properties.Settings.Default;
                if (!Directory.Exists(settings.FavoriteDirectory))
                {
                    Directory.CreateDirectory(settings.FavoriteDirectory);
                }
                string wall = Wallpaper;
                FileInfo file = new FileInfo(wall);
                System.IO.File.Copy(wall, settings.FavoriteDirectory + "\\" + file.Name, false);
                button5.Enabled = false;
            }
            catch (Exception)
            {
            }
        }
        private string FileSizeFormat(long size)
        {
            const int KB = 1024;
            const int MB = 1048576;
            const int GB = 1073741824;


            if (size < KB)
            {
                return string.Format("{0} bytes", size);
            }
            else if (size < MB)
            {
                return string.Format("{0} KB", (size / KB));
            }
            else if (size < GB)
            {
                return string.Format("{0:0.00} MB", (size / MB));
            }
            else
            {
                return string.Format("{0:0.00} GB", (size / GB));
            }
        }
    }
}
